'''
Created on Nov 19, 2010

@author: namikata
'''

from gameobjects.vector2 import Vector2
from pyglet.window import mouse

LEFT_BUTTON, MIDDLE_BUTTON, RIGHT_BUTTON = (0,1,2)

class Mouse(object):
    '''
    classdocs
    '''

    last_pressed = [False, False, False]
    current_pressed = [False, False, False]
    location = Vector2(0,0)
    current_wheel_scroll = 0
    last_wheel_scroll = 0
    
    def get_pressed(self,button):
        """0 = not pressed, 1 = pressed, 2 = hold """
        if self.current_pressed[button]:
            return int(self.current_pressed[button]) + int(self.last_pressed[button])
        else:
            return 0
   
    def get_scroll(self):
        return self.current_wheel_scroll - self.last_wheel_scroll
      
    def update_location(self,delta):
        self.location += Vector2(*delta)
        
    def update_buttons(self, buttons):
        current_buttons = [bool(buttons & mouse.LEFT), bool(buttons & mouse.MIDDLE), bool(buttons & mouse.RIGHT)]
        self.current_pressed = current_buttons
                
    def update_button_state(self, button, state):
        if button & mouse.LEFT:
            self.current_pressed[LEFT_BUTTON] = state
        elif button & mouse.MIDDLE:
            self.current_pressed[MIDDLE_BUTTON] = state
        elif button & mouse.RIGHT:
            self.current_pressed[RIGHT_BUTTON] = state
                
    def update_wheel(self, wheel_scroll):
        self.current_wheel_scroll += wheel_scroll
        
    def update(self):
        self.last_pressed = self.current_pressed
        self.last_wheel_scroll = self.current_wheel_scroll
        self.current_wheel_scroll = 0        